package com.hrm.controller;

import com.hrm.dao.HrmUserMapper;
import com.hrm.model.HrmUser;
import com.hrm.service.HrmUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


@Controller
@RequestMapping("/")
public class HrmUserController {
    @Autowired
    protected HrmUserService hrmUserService;
    /**
     * 打开登录页面
     * @return
     */
    @RequestMapping(value ="/")
    public ModelAndView hrmLoginManage(){
        ModelAndView mav = new ModelAndView("/hrmlogin");
        return mav;
    }

    /**
     * 用户登录验证
     * @param username
     * @param password
     * @return
     */
    @RequestMapping(value ="/checkPsw.post",method = RequestMethod.POST)
    @ResponseBody
    public Integer hrmLoginManage(String username, String password){
        HrmUser hrmUser = hrmUserService.login(username,password);
        if(null == hrmUser)
        return 0;
        else
        return hrmUser.getId();
    }

    /**
     * 打开用户信息页面
     * @return
     */
    @RequestMapping(value ="/user/hrmUserInfoManage.html")
    public ModelAndView hrmUserInfoManage(Integer id){
        ModelAndView mav = new ModelAndView("/hrmuserinfo");
        mav.addObject("hrmUser", hrmUserService.getHrmUser(id));
        return mav;
    }

    /**
     * 更新用户个人信息（其实就一个名字）
     * @param hrmUser
     * @return
     */
    @RequestMapping(value = "/updateUserInfo.post",method = RequestMethod.POST)
    @ResponseBody
    public Integer hrmUserInfoUpdateManage(HrmUser hrmUser){
        try
        {
            hrmUserService.updateUser(hrmUser);
            return 1;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 用户密码更改页面
     * @return
     */
    @RequestMapping(value ="/user/hrmPsdInfoManage.html")
    public ModelAndView hrmPsdInfoManage(Integer id){
        ModelAndView mav = new ModelAndView("/hrmpsdupdate");
        mav.addObject("hrmUser", hrmUserService.getHrmUser(id));
        return mav;
    }

    /**
     * 密码修改
     * @param OldPsw
     * @param NewPsw
     * @param id
     * @return
     */
    @RequestMapping(value = "/user/UpdatePsw.post",method = RequestMethod.POST)
    @ResponseBody
    public int updatePsw(String OldPsw,String NewPsw,Integer id){
        try
        {
            hrmUserService.updatePsw(OldPsw,NewPsw,id);
            return 1;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return 0;
        }
    }
}
